import 'package:flutter/material.dart';

class CustomSelectionDialog extends StatefulWidget {
  final String title;
  final List<String> items;
  final bool isMultiSelect;
  final List<String>? initialSelectedItems;

  const CustomSelectionDialog({
    Key? key,
    required this.title,
    required this.items,
    this.isMultiSelect = false,
    this.initialSelectedItems,
  }) : super(key: key);

  @override
  State<CustomSelectionDialog> createState() => _CustomSelectionDialogState();
}

class _CustomSelectionDialogState extends State<CustomSelectionDialog> {
  late List<bool> _selectedItems;

  @override
  void initState() {
    super.initState();
    _selectedItems = List.generate(
      widget.items.length,
      (index) => widget.initialSelectedItems != null && widget.initialSelectedItems!.contains(widget.items[index]),
    );
  }

  void _toggleAllSelection() {
    final allSelected = _selectedItems.every((isSelected) => isSelected);
    setState(() {
      _selectedItems = List.generate(
        widget.items.length,
        (index) => !allSelected,
      );
    });
  }

  void _toggleSelection(int index) {
    if (widget.isMultiSelect) {
      setState(() {
        _selectedItems[index] = !_selectedItems[index];
      });
    } else {
      setState(() {
        _selectedItems = List.generate(
          widget.items.length,
          (i) => i == index,
        );
      });
    }
  }

  List<String> _getSelectedItems() {
    return widget.items.asMap().entries.where((entry) => _selectedItems[entry.key]).map((entry) => entry.value).toList();
  }

  @override
  Widget build(BuildContext context) {
    return Dialog(
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(10),
      ),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          Padding(
            padding: const EdgeInsets.all(16),
            child: Text(
              widget.title,
              style: const TextStyle(
                fontSize: 18,
                fontWeight: FontWeight.bold,
              ),
            ),
          ),
          const Divider(height: 1),
          Expanded(
            child: ListView.builder(
              shrinkWrap: true,
              itemCount: widget.items.length,
              itemBuilder: (context, index) {
                final item = widget.items[index];
                final isSelected = _selectedItems[index];
                return MouseRegion(
                  cursor: SystemMouseCursors.click,
                  child: GestureDetector(
                    onTap: () => _toggleSelection(index),
                    child: Container(
                      color: isSelected ? Colors.blue.withOpacity(0.2) : null,
                      padding: const EdgeInsets.symmetric(
                        vertical: 12,
                        horizontal: 16,
                      ),
                      child: Row(
                        children: [
                          if (widget.isMultiSelect)
                            Checkbox(
                              value: isSelected,
                              onChanged: (value) => _toggleSelection(index),
                            )
                          else
                            Radio<bool>(
                              value: true,
                              groupValue: isSelected,
                              onChanged: (value) => _toggleSelection(index),
                            ),
                          const SizedBox(width: 8),
                          Text(item),
                        ],
                      ),
                    ),
                  ),
                );
              },
            ),
          ),
          const Divider(height: 1),
          Padding(
            padding: const EdgeInsets.all(16),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                if (widget.isMultiSelect)
                  TextButton(
                    onPressed: _toggleAllSelection,
                    child: const Text('全选/反选'),
                  ),
                Row(
                  children: [
                    TextButton(
                      onPressed: () => Navigator.pop(context),
                      child: const Text('取消'),
                    ),
                    const SizedBox(width: 16),
                    ElevatedButton(
                      onPressed: () => Navigator.pop(context, _getSelectedItems()),
                      child: const Text('确认'),
                    ),
                  ],
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
